package com.stylefeng.guns.modular.flagglory.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.common.persistence.model.Department;
import com.stylefeng.guns.common.persistence.dao.DepartmentMapper;
import com.stylefeng.guns.common.persistence.model.GloryUser;
import com.stylefeng.guns.common.persistence.model.Season;
import com.stylefeng.guns.core.exception.FrontException;
import com.stylefeng.guns.core.node.ZTreeNode;
import com.stylefeng.guns.modular.flagglory.dao.DepartmentDao;
import com.stylefeng.guns.modular.flagglory.service.IDepartmentService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.modular.flagglory.service.IGloryUserService;
import com.stylefeng.guns.modular.flagglory.service.ISeasonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * lg_部门表 服务实现类
 * </p>
 *
 * @author stylefeng123
 * @since 2018-01-31
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {
    @Autowired
    private DepartmentDao departmentDao;
    @Autowired
    private IGloryUserService userService;
    @Autowired
    private ISeasonService seasonService;

    public List<ZTreeNode> tree() {
        List<ZTreeNode> tree = departmentDao.tree();
        ZTreeNode zTreeNode = new ZTreeNode();
        zTreeNode.setName("顶级");
        zTreeNode.setId(0L);
        tree.add(zTreeNode);
        return tree;
    }

    @Override
    public Department getUserDepartment(Integer userId) {
        GloryUser user = userService.selectById(userId);
        Integer deptid = user.getDeptid();
        return selectById(deptid);
    }

    @Override
    public List<Map<String, Object>> selectDepartmentListById(Integer deptId) throws FrontException {
        Season defaultSeason = seasonService.getDefaultSeason();
        /* return departmentDao.selectDepartmentListById(deptId, defaultSeason.getId());*/
        EntityWrapper<Department> entityWrapper = new EntityWrapper<>();
        entityWrapper.where("pid=" + deptId);
        List<Department> departments = selectList(entityWrapper);
        List<Map<String, Object>> maps = new ArrayList<>();
        for (Department department : departments) {
            Integer id = department.getId();
            Map<String, Object> map = departmentDao.selectDepartmentById(id, defaultSeason.getId());
            map.put("id", id);
            map.put("deptName", map.getOrDefault("deptName", department.getFullname()));
            map.put("star", map.getOrDefault("star", 0));
            maps.add(map);
        }
        Collections.sort(maps, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> map1, Map<String, Object> map2) {
                int star = Integer.parseInt(map1.get("star").toString());
                int star2 = Integer.parseInt(map2.get("star").toString());
                return star2 - star;
            }
        });
        return maps;
    }

    @Override
    public List<Map<String, Object>> selectDepartmentList() throws FrontException {
        Season defaultSeason = seasonService.getDefaultSeason();
        EntityWrapper<Department> entityWrapper = new EntityWrapper<>();
        entityWrapper.where("pid=0");
        List<Department> departments = selectList(entityWrapper);
        List<Map<String, Object>> maps = new ArrayList<>();
        for (Department department : departments) {
            if (maps.size() >= 10) {
                break;
            }
            Integer id = department.getId();
            Map<String, Object> map = departmentDao.selectPDepartmentById(id, defaultSeason.getId());
            if (map == null) {
                map = new HashMap<>();
                map.put("star", 0);
                map.put("deptName", department.getFullname());
                map.put("id", id);
            }
            maps.add(map);
        }
        Collections.sort(maps, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> map1, Map<String, Object> map2) {
                int star = Integer.parseInt(map1.get("star").toString());
                int star2 = Integer.parseInt(map2.get("star").toString());
                return star2 - star;
            }
        });
//        return departmentDao.selectDepartmentList(defaultSeason.getId());
        return maps;
    }
}
